package com.sola.thymeleaf.controller;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.sola.thymeleaf.entity.User;

@Controller
public class LoginController {

	/**
	 * 参数是页面所绑定的 th:object="${user}"
	 * @param user
	 * @return
	 */
	@GetMapping("/")
	public String toLogin(User user){
		return "login";
	}
	
	@PostMapping("/login")
	public String checkForm(@Valid User user,BindingResult bindingResult){
		if(bindingResult.hasErrors()){
			return "login";
		}
		return "results";
	}
	
	@GetMapping("/normal")
	public String toNormalLogin(){
		return "normalLogin";
	}
	
	@PostMapping("/normalLogin")
	public String NormalCheckForm(@Valid User user,BindingResult bindingResult){
		if(bindingResult.hasErrors()){
			for(ObjectError objectError : bindingResult.getAllErrors()){
				System.out.println(objectError);
			}
			return "normalLogin";
		}
		return "NormalResults";
	}
	
}
